﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiamondDesktop.Presenters;
using DiamondDesktop.Model;
using DiamondDesktop;
using DiamondDesktop.utility;
namespace DiamondDesktop.Views
{
    /// <summary>
    /// Interaction logic for NewTView.xaml
    /// </summary>
    public partial class NewTView : UserControl
    {
        private bool isMust = false;
        private DateTime date = DateTime.Now;
        public NewTView(NewtaskPresenter presenter)
        {
            InitializeComponent();
            DataContext = presenter;
            
        }

        private void B_new_click(object sender, RoutedEventArgs e)
        {
            DiamondDesktop.Model.Task t = new DiamondDesktop.Model.Task();
            t.setTitle(this.TB_tasktitle.Text);
            t.setContent(this.TB_taskcontent.Text);
            t.setMust(isMust);
            if (this.TB_importance.Text != "")
                t.setPriority(Int32.Parse(this.TB_importance.Text));
            if (this.TB_time1.Text != "")
            {
                t.setActionStartTime(stringToDate(this.TB_time1.Text));
            }
            if (this.TB_time2.Text != "")
            {
                t.setActionEndTime(stringToDate(this.TB_time2.Text));
            }
            if (this.TB_period1.Text != "")
            {
                t.setTaskStartTime(stringToDate(this.TB_period1.Text));
            }
            else
            {
                DateTime d = System.DateTime.Now;
                t.setTaskStartTime(d);
            }
            if (this.TB_period2.Text != "")
            {
                t.setTaskEndTime(stringToDate(this.TB_period2.Text));
            }
            else
            {
                DateTime d = System.DateTime.Now;
                DateTime t1 = new DateTime(d.Year, d.Month, d.Day, 23, 59, 59);
                t.setTaskEndTime(t1);
            }
            Reminder.getInstance().addTask(t);

            ApplicationController.getInstance().ShowTaskList();
          
        }

        private void B_cancelnew_click(object sender, RoutedEventArgs e)
        {
            ApplicationController.getInstance().ShowCount();
        }

        private DateTime stringToDate(string s)
        {
            string[] ts = s.Split(".".ToCharArray());
            DateTime d = new DateTime(Int32.Parse(ts[0]), Int32.Parse(ts[1]), Int32.Parse(ts[2]), Int32.Parse(ts[3]), Int32.Parse(ts[4]), Int32.Parse(ts[5]));
            return d;
        }

        private void RB_mustdo_checked(object sender, RoutedEventArgs e)
        {
            isMust = true;
        }

        private void RB_mustdo_unchecked(object sender, RoutedEventArgs e)
        {
            isMust = false;
        }

        //被载入时的事件相应处理
        //设置任务开始事件为当前时间
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            TB_period1.Text = TimeUtility.DatetimeToString(DateTime.Now);
        }
    }
}
